name: Docs and Publish

on:
  push:
    branches:
      - master

jobs:
  docs_build:
    name: Docs-Build-And-Upload
    runs-on: ubuntu-latest
    # Skip running on forks since it won't have access to secrets
    if: github.repository == 'espressif/esp-protocols'

    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@master
        with:
          persist-credentials: false
          fetch-depth: 0

      - name: Generate docs
        run: |
          sudo apt-get update
          sudo apt-get -y install doxygen clang python3-pip
          python -m pip install breathe recommonmark esp-docs
          cd $GITHUB_WORKSPACE/components/esp_modem/docs
          ./generate_docs
          mkdir -p $GITHUB_WORKSPACE/docs/esp_modem
          cp -r html/. $GITHUB_WORKSPACE/docs/esp_modem

          cd $GITHUB_WORKSPACE/components/esp_websocket_client/docs
          ./generate_docs
          mkdir -p $GITHUB_WORKSPACE/docs/esp_websocket_client
          cp -r html/. $GITHUB_WORKSPACE/docs/esp_websocket_client

          cd $GITHUB_WORKSPACE/components/mdns/docs
          ./generate_docs
          mkdir -p $GITHUB_WORKSPACE/docs/mdns/en
          mkdir -p $GITHUB_WORKSPACE/docs/mdns/zh_CN
          cp -r html_en/. $GITHUB_WORKSPACE/docs/mdns/en
          cp -r html_zh_CN/. $GITHUB_WORKSPACE/docs/mdns/zh_CN

          cd $GITHUB_WORKSPACE/components/asio/docs
          ./generate_docs
          mkdir -p $GITHUB_WORKSPACE/docs/asio
          cp -r html/. $GITHUB_WORKSPACE/docs/asio

          cd $GITHUB_WORKSPACE/docs
          touch .nojekyll
          echo '<a href="esp_modem/index.html">esp-modem</a><br>' > index.html
          echo '<a href="esp_websocket_client/index.html">esp-websocket-client</a><br>' >> index.html
          echo '<a href="asio/index.html">ASIO</a><br>' >> index.html
          echo '<a href="mdns/en/index.html">mDNS_en</a><br>' >> index.html
          echo '<a href="mdns/zh_CN/index.html">mDNS_zh_CN</a><br>' >> index.html


      - name: Upload components to component service
        uses: espressif/github-actions/upload_components@master
        with:
          directories: "components/esp_modem;components/esp_websocket_client;components/mdns;components/asio"
          namespace: "espressif"
          api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}

      - name: Deploy generated docs
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages
          folder: docs
